const MYSQL = require("../../sql/mysql");
const MYSQLConfig = require("../../sql/database");
const SQLName = MYSQLConfig.datasheetName.user_token;
/**
 * 获取token列表
 * @param {object} params
 * @param {string} params.user 用户
 * @return {Promise<{code:number,msg:string} | {code:number,msg:string}>}
 */
module.exports = async (params = {}) => {
  try {
    let {
      user,
      page = 1, // 当前页
      limit = 10, // 每页记录数量
      where = {}, // 筛选条件
    } = params;

    limit = limit < 1 ? (limit = 1) : limit;
    page = page < 1 ? (page = 1) : page;
    let query = `
      SELECT * FROM ${SQLName}
    `;
    if (user != undefined) {
      query += `WHERE user = ${user}`;
    }
    let result = await MYSQL.promise().query(query);
    // 获取数据总数 =====
    let totalResult = await MYSQL.promise().query(
      `
      SELECT Count(*) AS total FROM ${SQLName}
      `
    );
    let total = totalResult[0][0].total;
    let list = result[0];
    return {
      code: 0,
      msg: "获取成功",
      data: {
        page,
        limit,
        data: list,
        total,
        list,
      },
    };
  } catch (error) {
    throw error;
  }
};
